package org.openrbac.rbac.utils;


import org.openrbac.rbac.ubean.MailBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * @author 梁富贵
 */
@Configuration
public class MailUtil {
    //邮件发送者
    @Value("${spring.mail.username}")
    private String MAIL_SENDER;


    @Autowired
    private JavaMailSender javaMailSender;

    private Logger logger = LoggerFactory.getLogger(MailUtil.class);


    public void sendSimpleMail(MailBean mailBean) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //发送者
            mailMessage.setFrom(MAIL_SENDER);
            //接收者
            mailMessage.setTo(mailBean.getReceiver());
            //邮件标题
            mailMessage.setSubject(mailBean.getSubject());
            //邮件内容
            mailMessage.setText(mailBean.getContent());
            //发送邮箱
            javaMailSender.send(mailMessage);
        } catch (Exception e) {
            logger.error("邮件发送失败", e.getMessage());
        }
    }
}